package com.zhenhunfan.service;

import com.zhenhunfan.HelloProto;
import com.zhenhunfan.HelloServiceGrpc;
import io.grpc.stub.StreamObserver;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author zhanghong
 * @date 2023/7/17
 */
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
    /**
     * Java并不以返回值对应service中的returns，因为考虑到底层实现的4中通信方式
     * 一元RPC，简单RPC
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        //----------1.接受客户端的请求参数----------
        String name = request.getName();
        //----------2.业务处理----------
        System.out.println("name 参数值：" + name);

        //----------3.封装响应----------
        //-----3.1 创建响应对象的构建者-----
        HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
        //-----3.2 填充数据-----
        builder.setResult(name + "你好!");
        //-----3.3 封装响应对象-----
        HelloProto.HelloResponse helloResponse = builder.build();

        responseObserver.onNext(helloResponse);
        responseObserver.onCompleted();
    }

    /**
     * 服务端流式RPC
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void c2ss(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        //----------1.接受客户端的请求参数----------
        String name = request.getName();
        //----------2.业务处理----------
        System.out.println("name 参数值：" + name);

        //----------3.封装响应----------
        for (int i = 0; i < 9; i++) {
            //-----3.1 创建响应对象的构建者-----
            HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
            //-----3.2 填充数据-----
            builder.setResult("处理的结果" + i);
            HelloProto.HelloResponse helloResponse = builder.build();
            responseObserver.onNext(helloResponse);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        responseObserver.onCompleted();
    }

    /**
     * 客户端流式RPC
     *
     * @param responseObserver 响应
     * @return 请求
     */
    @Override
    public StreamObserver<HelloProto.HelloRequest> cs2s(StreamObserver<HelloProto.HelloResponse> responseObserver) {
        return new StreamObserver<HelloProto.HelloRequest>() {

            @Override
            public void onNext(HelloProto.HelloRequest helloRequest) {
                System.out.println("客户端流式RPC：" + helloRequest.getName());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("客户端所有消息都已经发送了");
                //提供响应 响应的目的：当接收了全部客户端提交的信息，并处理后，提供响应
                //-----1.1 创建响应对象的构建者-----
                HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
                //-----1.2 填充数据-----
                builder.setResult("处理的结果：客户端所有消息都已经发送了");
                HelloProto.HelloResponse helloResponse = builder.build();
                responseObserver.onNext(helloResponse);
                responseObserver.onCompleted();
            }
        };
    }

    /**
     * 双向流式RPC
     *
     * @param responseObserver 响应观察者
     * @return 请求观察者
     */
    @Override
    public StreamObserver<HelloProto.HelloRequest> cs2ss(StreamObserver<HelloProto.HelloResponse> responseObserver) {
        return new StreamObserver<HelloProto.HelloRequest>() {
            @Override
            public void onNext(HelloProto.HelloRequest helloRequest) {
                System.out.println("接收到客户端提交的消息：" + helloRequest.getName());
                responseObserver.onNext(HelloProto.HelloResponse.newBuilder()
                        .setResult("服务端发给客户端消息："+helloRequest.getName()).build());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("接收到了所有的请求消息");
                responseObserver.onCompleted();
            }
        };
    }

}
